package multithread;

import com.google.common.util.concurrent.RateLimiter;
import multithread.thredpool.ThreadpollFutureTaskCall;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RateLimiterMain {
     static RateLimiter rateLimiter = RateLimiter.create(1);
        public static void main(String[] args) {
//            System.out.println(rateLimiter.acquire(5));
//            System.out.println(rateLimiter.acquire(2));
//            System.out.println(rateLimiter.acquire(1));
            ThreadPoolExecutor    executor = new ThreadPoolExecutor(20, 20, 200, TimeUnit.MILLISECONDS,
                    new ArrayBlockingQueue<Runnable>(20));
            List<ThreadpollFutureTaskCall.Wrapper> list = new ArrayList<ThreadpollFutureTaskCall.Wrapper>();
            for(int i=0;i<20;i++){
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        rateLimiter.acquire(); // 也许需要等待
                        testAcquire();
                    }
                });
                //System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+ executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());
            }
        }

        private static void testAcquire(){
            //System.out.println("start testAcquire");
            //尝试获取令牌
            //if(rateLimiter.tryAcquire()){
                //模拟业务执行500毫秒
                System.out.println("aceess success [" +new Date() + "]");
//           }else{
//                System.out.println("23232");
//            }
        }

}
